//
//  ITAppStartManager.m
//  GLPFinance
//
//  Created by ITBE on 2016/11/23.
//  Copyright © 2016年 intube. All rights reserved.
//

#import "ITAppStartManager.h"
#import <AVFoundation/AVFoundation.h>
#import "AppDelegate.h"
#import "ITGeneralManager.h"
#import "AFNetworkReachabilityManager.h"

#import "ITNetworkMonitorManager.h"

#import "YYKit.h"

//#import <GTSDK/GeTuiSdk.h>

//#import "AppDelegate+AppService.h"

#define DeviceTokenService @"com.client.videochat.deviceTokenService"
#define DeviceTokenAccount @"com.client.videochat.deviceTokenAccount"

#define DidShowGeneralKey @"DidShowGeneralKey"
#define DidShowGuideKey @"DidShowGuideKey"

@interface ITAppStartManager()<UITabBarControllerDelegate>{
    
}
@end
@implementation ITAppStartManager
+(instancetype)shareManager
{
    static ITAppStartManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[ITAppStartManager alloc] init];
    });
    return manager;
}


/**
 app启动时处理事件
 */
-(void)startApp
{
    
    [self setHomeView];
    /*
    WEAKSELF
    //[[BTGeneralManager defaultManager] getGlovalVarWithVersion];
    ITUserInfo *host = ITUserManager.shareManager.member;
    if (host) {//刷新用户信息
        [ITTrackManager.shareManager updateMemberId:host.memberId];
        [ITTrackManager.shareManager updateGender:@(host.gender)];
        [ITTrackManager.shareManager updateCampaign:host.adSource];
        NSLog(@"memberId = %@",host.memberId);
        [ITGeneralManager.defaultManager getCommonConfigInfo:^(id config) {
            [weak_self setHomeView];
        }];
    }else{
        [ITGeneralManager.defaultManager getCommonConfigInfo:^(id config) {

        }];
    }
     //*/
}



-(void)setNavigationColor:(UINavigationController *)nav
{
    if (nav) {
        //[nav setNavigationViewColor:[UIColor whiteColor]];
//        [_navigationController setStatusBarStyle:UIStatusBarStyleLightContent];
    }
}

/**
 返回当前navigationController的最上面的ViewController
 
 @return ViewController
 */
-(UIViewController *)topViewController
{
    return _navigationController?_navigationController.topViewController:nil;
}

-(void)generateTabBarController{

    _tabBarController = [[ITTabBarController alloc] init];
}



-(void)setHomeView{
    UIWindow *window = [(AppDelegate *)[UIApplication sharedApplication].delegate window];
    
    [self generateTabBarController];
    _navigationController = [[BaseNavigationController alloc] initWithRootViewController:_tabBarController];
    [_navigationController setNavigationBarHidden:YES];
    _navigationController.navigationBar.tintColor = COLOR_333333;
    _navigationController.navigationBar.translucent = NO;
    
    [window setRootViewController:_navigationController];
    [_tabBarController setSelectedIndex:0];
    
    [self setNavigationColor:_navigationController];
    
    //[BTOrientationManager.sharedInsatnce rotateInterfaceOrientation:(UIInterfaceOrientationPortrait)];
    //[_navigationController.navigationBar setShadowImage:[UIImage new]];

}

-(void)pushHomeView
{
    [self generateTabBarController];
    [_navigationController setNavigationBarHidden:YES];
    [_navigationController pushViewController:_tabBarController animated:YES];
    [_tabBarController setSelectedIndex:0];
    [self setNavigationColor:_navigationController];
}

-(void)selectTabBarIndex:(NSInteger)index
{
    if (_tabBarController) {
        [_tabBarController setSelectedIndex:index];
    }else{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self selectTabBarIndex:index];
        });
    }
}

-(void)setBadgeNumber:(int)badge
{
    UITabBar *tabBar = _tabBarController.tabBar;
    UITabBarItem *item = [tabBar.items objectAtIndex:1];
    NSString *badgeStr;
    if (badge > 99) {
        badgeStr = @"99+";
    }else if(badge > 0){
        badgeStr = [NSString stringWithFormat:@"%d",badge];
    }else{
        badgeStr = nil;
    }
    [item setBadgeValue:badgeStr];
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:badge];
    //[GeTuiSdk setBadge:badge];
}

-(CGRect)tabBarItemRectToScreenWithIndex:(NSInteger)index
{
    if (_tabBarController) {
        if (index < _tabBarController.tabBar.items.count) {
            CGFloat start = (3.0*(index+1)-2.0)* GDeviceWidth/(_tabBarController.tabBar.items.count*3);
            CGFloat itemWith = 1 * GDeviceWidth / (3.0f * _tabBarController.tabBar.items.count);
            if (_tabBarController.tabBar.items.count > 5) {
                itemWith = 1 * GDeviceWidth / 15.0f;
            }
            return CGRectMake(start, GDeviceHeight - [UIDevice safeAreaTabbarHeight], itemWith, [UIDevice safeAreaTabbarHeight]);
        }
    }
    return CGRectZero;
}

-(void)selectHomeView
{
    if (_tabBarController) {
        [_tabBarController setSelectedIndex:0];
    }
}


-(void)resetAppView
{
    [_navigationController setViewControllers:@[_tabBarController] animated:YES];
    [_tabBarController setSelectedIndex:0];
}

@end
